package com.shop.server.controller;

import com.shop.common.constant.MessageConstant;
import com.shop.common.entity.CommonResult;
import com.shop.server.pojo.RegisterUser;
import com.shop.server.service.CodeService;
import com.shop.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ghy
 * @version 1.0
 * @date 2020-10-29
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class RegisterController {

    @Autowired
    CodeService codeService;

    @Autowired
    UserService userService;

    /**
     * 发送验证码?
     * @param phone
     * @return
     */
    @GetMapping("/code")
    public CommonResult<String> code(String phone) {
        try {
            if(codeService.sendCode(phone)){
                return new CommonResult<>("验证码发送成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonResult<>(false, MessageConstant.ERROR, "网络延迟，验证码发送失败！");
    }

    /**
     * 注册
     *    参数：手机号、验证码、密码
     * @return
     */
    @PostMapping("/register")
    public CommonResult register(@RequestBody RegisterUser user) {

        try {
            //2.2 对上：进入注册流程
            log.info("进入注册流程..{}", user);
            return userService.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(MessageConstant.REG_FAIL, "网络延迟，请稍后重试！");
        }
    }


}
